package Foundation;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import DomainPackage.Havalimani;
import DomainPackage.Musteri;
import DomainPackage.Pilot;
import DomainPackage.SatisElemani;
import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;

public class HavalimaniMapper implements IMapper {

	@Override
	public Object get(int ID) {
		
		Havalimani havalimani = new Havalimani(-1);
		ResultSet rs = DataConnector.getInstance().query("select * from havalimani where havalimaniid = " + ID + "");
		
		if (rs != null){
            
            try {
            		rs.next();
                    havalimani.setHavalimaniID(Integer.parseInt(rs.getString(1)));
                    havalimani.setHavalimaniAdi(rs.getString(2));
                    havalimani.setPistSayisi(Integer.parseInt(rs.getString(3)));
                    havalimani.setUcakKapasitesi(Integer.parseInt(rs.getString(4)));
                    havalimani.setKuleSayisi(Integer.parseInt(rs.getString(5)));
            		
                             return havalimani;
                   
            } catch (NumberFormatException e) {
                    System.out.println("havalimaniMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("havalimaniMapper: sql problem");
                    e.printStackTrace();
            }
    }
    else
            System.out.println("no havalimani with given ID");
		return null;
	}

	@Override
	public int put(Object object) {
		
		Havalimani havalimani=(Havalimani) object;
		String dt = "";

        if(!DataConnector.getInstance().update("INSERT INTO havalimani "+
                "(havalimaniid, havalimaniadi, pistsayisi, ucakkapasitesi, kulesayisi) " +
                "values ('"+havalimani.getHavalimaniID()+"', '"+
                havalimani.getHavalimaniAdi()+"', '"+
                havalimani.getPistSayisi()+"', '"+
                havalimani.getUcakKapasitesi()+"', '"+
                havalimani.getKuleSayisi()+"')")){
        	System.out.println("error");
        }
		
     return 0;

	}

	@Override
	public List<Object> getAll() {
		
		List<Object> allHavalimaniList = new ArrayList<Object>();
    	ResultSet rs = DataConnector.getInstance().query("select * from havalimani;");
    	int numberOfRows=0;
		try {
			ResultSetMetaData rsMetaData = rs.getMetaData();
            numberOfRows = rsMetaData.getColumnCount()+1;
		} catch (SQLException e1) {
			
			e1.printStackTrace();
		}
    try {
    	if (rs != null){
            while(rs.next()){
                   
                    Havalimani havalimani= new Havalimani(-1);
                 	
                    	havalimani.setHavalimaniID(Integer.parseInt(rs.getString(1)));
                        havalimani.setHavalimaniAdi(rs.getString(2));
                        havalimani.setPistSayisi(Integer.parseInt(rs.getString(3)));
                        havalimani.setUcakKapasitesi(Integer.parseInt(rs.getString(4)));
                        havalimani.setKuleSayisi(Integer.parseInt(rs.getString(5)));
                            allHavalimaniList.add(havalimani);
              
            }
    	}
    }
    catch(Exception e){
    	e.printStackTrace();
    }
    	return allHavalimaniList;
	
	}

}
